package com.haoshuang.zhouzhoubang.ui.page.help.login

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.databinding.ActivityLoginHelpBinding
import com.haoshuang.zhouzhoubang.model.bean.LoginHelpBean
import com.haoshuang.zhouzhoubang.ui.page.help.detail.HelpDetailActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.browse
import com.mufeng.libs.utils.callPhone
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical


/**
 *
 */
class LoginHelpActivity : BaseVMActivity<LoginHelpViewModel, ActivityLoginHelpBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<LoginHelpActivity>()
        }
    }

    private var tel: String? = null
    private val data = arrayListOf<LoginHelpBean>()
    private val adapter = LoginHelpAdapter(data)

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "登录帮助"
        binding.titleBar.leftImageView().click { finish() }

        binding.recyclerView.vertical().adapter = adapter
        adapter.setOnItemClick { view, position, item ->
            // 打开帮助详情页
            startPage<HelpDetailActivity>("title" to item?.title, "url" to "${URLConstants.helpUrl}${item?.id}")
        }

        binding.tvContactService.clickWithTrigger {
            // 打开联系客服页面
            if (tel != null && tel!!.isNotEmpty()){
                callPhone(tel!!)
            }
        }

    }

    override fun initData() {
        vm.loginHelpList()
    }

    override fun startObserve() {
        vm.loginHelpListData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                data.clear()
                data.addAll(it?.data?.list ?: emptyList())
                adapter.notifyDataSetChanged()
                tel = it?.data?.tel ?: ""
            }
        }
    }

}